{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Chalkboard XOR Game"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Hard"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #bit-manipulation #brainteaser #array #math #game-theory"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #位运算 #脑筋急转弯 #数组 #数学 #博弈"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: xorGame"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #黑板异或游戏"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>黑板上写着一个非负整数数组 <code>nums[i]</code> 。</p>\n",
    "\n",
    "<p>Alice 和 Bob 轮流从黑板上擦掉一个数字，Alice 先手。如果擦除一个数字后，剩余的所有数字按位异或运算得出的结果等于 <code>0</code> 的话，当前玩家游戏失败。&nbsp;另外，如果只剩一个数字，按位异或运算得到它本身；如果无数字剩余，按位异或运算结果为&nbsp;<code>0</code>。</p>\n",
    "\n",
    "<p>并且，轮到某个玩家时，如果当前黑板上所有数字按位异或运算结果等于 <code>0</code> ，这个玩家获胜。</p>\n",
    "\n",
    "<p>假设两个玩家每步都使用最优解，当且仅当 Alice 获胜时返回 <code>true</code>。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入:</strong> nums = [1,1,2]\n",
    "<strong>输出:</strong> false\n",
    "<strong>解释:</strong> \n",
    "Alice 有两个选择: 擦掉数字 1 或 2。\n",
    "如果擦掉 1, 数组变成 [1, 2]。剩余数字按位异或得到 1 XOR 2 = 3。那么 Bob 可以擦掉任意数字，因为 Alice 会成为擦掉最后一个数字的人，她总是会输。\n",
    "如果 Alice 擦掉 2，那么数组变成[1, 1]。剩余数字按位异或得到 1 XOR 1 = 0。Alice 仍然会输掉游戏。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入:</strong> nums = [0,1]\n",
    "<strong>输出:</strong> true\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入:</strong> nums = [1,2,3]\n",
    "<strong>输出:</strong> true\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 1000</code></li>\n",
    "\t<li><code>0 &lt;= nums[i] &lt; 2<sup>16</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [chalkboard-xor-game](https://leetcode.cn/problems/chalkboard-xor-game/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [chalkboard-xor-game](https://leetcode.cn/problems/chalkboard-xor-game/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,1,2]', '[0,1]', '[1,2,3]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        ans = reduce(lambda x,y: x^y, nums)\n",
    "        return ans==0 or len(nums)&1!=1\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        if len(nums) % 2 == 0:\n",
    "            return True\n",
    "        \n",
    "        xorsum = reduce(xor, nums)\n",
    "        return xorsum == 0\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        if len(nums) % 2 == 0:\n",
    "            return True\n",
    "        # 采用异或运算\n",
    "        xorsum = reduce(xor, nums)\n",
    "        return xorsum == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        # 对于Alice和Bob，每次操作时，待操作的队列都有相同的奇偶性\n",
    "        # n为偶数时，Alice如果不能获胜，说明对于某一位一定有奇数个1，换句话说，可擦除的数中一定包含那一位为0的数\n",
    "        # 只要Alice每次选择那个数，就能保持不败（当Alice排除了所有的0之后，Bob只能移除一个1，这样会导致这个数组xor为0）\n",
    "        return len(nums) % 2 == 0 or reduce(xor, nums) == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        if n % 2 == 0:\n",
    "            return True\n",
    "        x = 0\n",
    "        for num in nums:\n",
    "            x ^= num\n",
    "        return x == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        if len(nums) % 2 == 0:\n",
    "            return True\n",
    "        return reduce(xor, nums) == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        ans=0\n",
    "        if(len(nums)&1):\n",
    "            for i in nums:ans^=i\n",
    "        return ans==0\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        if len(nums) & 1 == 0:\n",
    "            return True\n",
    "        v = 0\n",
    "        for x in nums:\n",
    "            v ^= x\n",
    "        if v == 0:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums:List[int]) -> bool:\n",
    "    # 如果数组长度为偶数，Alice 一定获胜\n",
    "        if len(nums) % 2 == 0:\n",
    "            return True\n",
    "        \n",
    "        # 如果数组长度为奇数，且所有数字的异或结果为 0，Alice 也获胜\n",
    "        xor_result = 0\n",
    "        for num in nums:\n",
    "            xor_result ^= num\n",
    "        \n",
    "        if xor_result == 0:\n",
    "            return True\n",
    "        \n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        if n % 2 == 0:\n",
    "            return True\n",
    "        s = 0\n",
    "        for x in nums:\n",
    "            s ^= x\n",
    "        return s == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        return len(nums) % 2 == 0 or reduce(xor, nums) == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        ans = reduce(lambda x,y: x^y, nums)\n",
    "        return ans==0 or len(nums)&1!=1\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "      return len(nums) % 2 == 0 or reduce(lambda x, y: x^y, nums) == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, arr):\n",
    "        xor = 0\n",
    "        for a in arr:\n",
    "            xor ^= a\n",
    "        return xor == 0 or len(arr) % 2 == 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        xor=0\n",
    "        for i in nums:\n",
    "            xor=xor^i\n",
    "        if xor==0:\n",
    "            return True\n",
    "        if len(nums)%2==0:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        # 博弈函数 什么博弈\n",
    "        if len(nums) & 1 == 0:\n",
    "            return True\n",
    "        i = 0\n",
    "        for n in nums:\n",
    "            i ^= n\n",
    "        return i == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        return reduce(lambda x,y: x^y, nums)==0 or len(nums)&1!=1\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        d = {}\n",
    "        r = 0\n",
    "        for x in nums:\n",
    "            d[x] = d.get(x, 0) + 1\n",
    "            r ^= x\n",
    "        a = 0\n",
    "        for k, v in d.items():\n",
    "            if v % 2 != 0:\n",
    "                a += 1\n",
    "        return (a % 2 == 0) or (r == 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        result=0\n",
    "        for i in range(len(nums)):\n",
    "            result^=nums[i]\n",
    "        return len(nums)%2==0 or result==0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        return len(nums) % 2 == 0 or reduce(xor, nums) == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        ans = reduce(lambda x,y: x^y, nums)\n",
    "        return ans==0 or len(nums)&1!=1\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        return not (len(nums) & 1) or not reduce(xor, nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        if len(nums)%2==0:\n",
    "            return True\n",
    "        \n",
    "        res = 0\n",
    "        for num in nums:\n",
    "            res^=num\n",
    "        return res == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        nums.sort()\n",
    "        x=nums[-1]\n",
    "        long=0\n",
    "        while x>0:\n",
    "            x=x//2\n",
    "            long=long+1\n",
    "        ans=[0]*(long)\n",
    "        for item in nums:\n",
    "            k=long-1\n",
    "            while k>=0:\n",
    "                ans[k]=(ans[k]+item//2**k)%2\n",
    "                item=item%2**k\n",
    "                k=k-1\n",
    "            \n",
    "        \n",
    "        if ans==[0]*long:\n",
    "            return True\n",
    "        else:\n",
    "            if len(nums)%2==1:\n",
    "                return False\n",
    "            else:\n",
    "                return True\n",
    "            \n",
    "        \n",
    "\n",
    "\n",
    "\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        if len(nums) % 2 == 0:\n",
    "            return True\n",
    "        else:\n",
    "            xor = 0\n",
    "            for num in nums:\n",
    "                xor ^= num\n",
    "            return xor == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "from functools import reduce, partial, lru_cache\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        \"\"\"\n",
    "\n",
    "        :param nums:\n",
    "        :return:\n",
    "        \"\"\"\n",
    "        if len(nums) % 2 == 0:\n",
    "            return True\n",
    "        return reduce(lambda x, y: x ^ y, nums) == 0\n",
    "\n",
    "\n",
    "a = Solution()\n",
    "print(a.xorGame([1, 1, 2]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from functools import reduce\n",
    "import operator\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums):\n",
    "        return len(nums) % 2 == 0 or reduce(operator.xor, nums) == 0\n",
    "\n",
    "# 示例\n",
    "solution = Solution()\n",
    "nums1 = [1, 1, 2]\n",
    "nums2 = [0, 1]\n",
    "nums3 = [1, 2, 3]\n",
    "\n",
    "print(solution.xorGame(nums1))  # false\n",
    "print(solution.xorGame(nums2))  # true\n",
    "print(solution.xorGame(nums3))  # true\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        val = 0\n",
    "        for num in nums: val ^= num\n",
    "        if val == 0: return True\n",
    "        if len(nums) % 2 == 0: return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        if len(nums)%2 == 0:\n",
    "            return True\n",
    "        xor = 0\n",
    "        for n in nums:\n",
    "            xor ^= n\n",
    "        return xor == 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums):\n",
    "        # 如果数组长度是偶数，Alice 总是能赢\n",
    "        if len(nums) % 2 == 0:\n",
    "            return True\n",
    "\n",
    "        # 计算所有数字的异或结果\n",
    "        xor_result = 0\n",
    "        for num in nums:\n",
    "            xor_result ^= num\n",
    "\n",
    "        # 如果结果为0，Alice 获胜\n",
    "        return xor_result == 0\n",
    "\n",
    "# 示例\n",
    "solution = Solution()\n",
    "nums1 = [1, 1, 2]\n",
    "nums2 = [0, 1]\n",
    "nums3 = [1, 2, 3]\n",
    "\n",
    "print(solution.xorGame(nums1))  # false\n",
    "print(solution.xorGame(nums2))  # true\n",
    "print(solution.xorGame(nums3))  # true\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        l = len(nums)\n",
    "        res = 0\n",
    "        for i in range(l):\n",
    "            res ^= nums[i]\n",
    "        if res == 0:\n",
    "            return True\n",
    "        else:\n",
    "            return True if l % 2 == 0 else False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        return len(nums) % 2 == 0 or reduce(xor,nums) == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        if len(nums)%2==0:\n",
    "            return True\n",
    "        #异或运算\n",
    "        xorsum=reduce(xor,nums)\n",
    "        return xorsum==0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "# File       : Q810.py\n",
    "# Time       ：2023/9/23 15:45\n",
    "# Author     ：author name\n",
    "# version    ：python 3\n",
    "# Description：\n",
    "\"\"\"\n",
    "\n",
    "\"\"\"\n",
    "810. 黑板异或游戏\n",
    "困难\n",
    "150\n",
    "相关企业\n",
    "黑板上写着一个非负整数数组 nums[i] 。\n",
    "\n",
    "Alice 和 Bob 轮流从黑板上擦掉一个数字，Alice 先手。如果擦除一个数字后，剩余的所有数字按位异或运算得出的结果等于 0 的话，当前玩家游戏失败。 另外，如果只剩一个数字，按位异或运算得到它本身；如果无数字剩余，按位异或运算结果为 0。\n",
    "\n",
    "并且，轮到某个玩家时，如果当前黑板上所有数字按位异或运算结果等于 0 ，这个玩家获胜。\n",
    "\n",
    "假设两个玩家每步都使用最优解，当且仅当 Alice 获胜时返回 true。\n",
    "\n",
    " \n",
    "\n",
    "示例 1：\n",
    "\n",
    "输入: nums = [1,1,2]\n",
    "输出: false\n",
    "解释: \n",
    "Alice 有两个选择: 擦掉数字 1 或 2。\n",
    "如果擦掉 1, 数组变成 [1, 2]。剩余数字按位异或得到 1 XOR 2 = 3。那么 Bob 可以擦掉任意数字，因为 Alice 会成为擦掉最后一个数字的人，她总是会输。\n",
    "如果 Alice 擦掉 2，那么数组变成[1, 1]。剩余数字按位异或得到 1 XOR 1 = 0。Alice 仍然会输掉游戏。\n",
    "示例 2:\n",
    "\n",
    "输入: nums = [0,1]\n",
    "输出: true\n",
    "示例 3:\n",
    "\n",
    "输入: nums = [1,2,3]\n",
    "输出: true\n",
    " \n",
    "\n",
    "提示：\n",
    "\n",
    "1 <= nums.length <= 1000\n",
    "0 <= nums[i] < 2 ** 16\n",
    "\n",
    "\"\"\"\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        xor_sum = nums[0]\n",
    "        for i in range(1, len(nums)):\n",
    "            xor_sum ^= nums[i]\n",
    "        return len(nums) % 2 == 0 or xor_sum == 0\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        result=0\n",
    "        for i in range(len(nums)):\n",
    "            result^=nums[i]\n",
    "        return len(nums)%2==0 or result==0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums):\n",
    "    # 如果数组长度为偶数，Alice 一定获胜\n",
    "        if len(nums) % 2 == 0:\n",
    "            return True\n",
    "        \n",
    "        # 如果数组长度为奇数，且所有数字的异或结果为 0，Alice 也获胜\n",
    "        xor_result = 0\n",
    "        for num in nums:\n",
    "            xor_result ^= num\n",
    "        \n",
    "        if xor_result == 0:\n",
    "            return True\n",
    "        \n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums):\n",
    "        xor_result = 0\n",
    "        for num in nums:\n",
    "            xor_result ^= num\n",
    "        \n",
    "        return xor_result == 0 or len(nums) % 2 == 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        res = 0\n",
    "        for i in nums:\n",
    "            res ^= i\n",
    "        if not res:return True\n",
    "        return not len(nums) % 2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums):\n",
    "        if len(nums) % 2 == 0:\n",
    "            return True\n",
    "        \n",
    "        xor_result = 0\n",
    "        for num in nums:\n",
    "            xor_result ^= num\n",
    "\n",
    "        if xor_result == 0:\n",
    "            return True\n",
    "        \n",
    "        # If the xor result is non-zero and the length is odd, Alice will lose\n",
    "        return False\n",
    "\n",
    "# Test cases\n",
    "solution = Solution()\n",
    "nums1 = [1, 1, 2]\n",
    "nums2 = [0, 1]\n",
    "nums3 = [1, 2, 3]\n",
    "\n",
    "results = {\n",
    "    \"Test1\": solution.xorGame(nums1),  # False\n",
    "    \"Test2\": solution.xorGame(nums2),  # True\n",
    "    \"Test3\": solution.xorGame(nums3)   # True\n",
    "}\n",
    "\n",
    "results\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        if n % 2 == 0:\n",
    "            return True\n",
    "        x = 0\n",
    "        for i in nums:\n",
    "            x ^= i\n",
    "        return x == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        judge = nums[0]\n",
    "        for i in range(1, len(nums)):\n",
    "            judge = judge ^ nums[i]\n",
    "        if judge == 0:\n",
    "            return True\n",
    "        else:\n",
    "            if len(nums) % 2 == 1:\n",
    "                return False\n",
    "            else:\n",
    "                return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def xorGame(self, nums: List[int]) -> bool:\r\n",
    "        if len(nums)%2==0:\r\n",
    "            return True\r\n",
    "        xor=0\r\n",
    "        for n in nums:\r\n",
    "            xor^=n\r\n",
    "        return xor==0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        if len(nums)%2==0:\n",
    "            return True\n",
    "        res = 0\n",
    "        for x in nums:\n",
    "            res ^= x\n",
    "        return res==0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        return reduce(xor, nums) == 0 or len(nums) % 2 == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        if len(nums) % 2 == 0:\n",
    "            return True\n",
    "        \n",
    "        xorsum = reduce(xor, nums)\n",
    "        return xorsum == 0\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        a = 0\n",
    "        for num in nums:\n",
    "            a = a ^ num\n",
    "        if a == 0:\n",
    "            return True\n",
    "        return (n % 2 == 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def xorGame(self, nums: List[int]) -> bool:\n",
    "        xor_sum = 0\n",
    "        for num in nums:\n",
    "            xor_sum ^= num\n",
    "        \n",
    "        return xor_sum == 0 or len(nums) % 2 == 0\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
